//
//  WZAuthController.m
//  weibo
//
//  Created by mac on 16/2/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "WZAuthController.h"
#import "WZUserViewModel.h"
#import "WZUserInfoModel.h"
@interface WZAuthController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@end

@implementation WZAuthController

-(UIWebView *)webView{
    if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
        
        _webView.backgroundColor = [UIColor redColor];
        
    }
    return _webView;
}

-(void)loadView{
    self.view = self.webView;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.delegate = self;
    [self loadWebView];
    
    [self setNavBarItem];
    
}
-(void)loadWebView{

    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=517511500&redirect_uri=http://www.baidu.com&response_type=code"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

-(void)setNavBarItem{
    
    [self.navigationController.navigationBar setTintColor:[UIColor orangeColor]];
    
    UIBarButtonItem *dismissItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(desmissClick)];
    
    self.navigationItem.leftBarButtonItem = dismissItem;
    UIBarButtonItem *autoItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(autoClick)];
    
    self.navigationItem.rightBarButtonItem = autoItem;
    
}
-(void)desmissClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)autoClick{
    NSString *js = @"document.getElementById(\"userId\").value = \"1129830296@qq.com\";document.getElementById(\"passwd\").value = \"wen15233217989\";";
    
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始:");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
     NSLog(@"结束");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
     NSLog(@"失败");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSLog(@"%@",request.URL);
    NSString *str = request.URL.absoluteString;
    if ([str hasPrefix:@"http://www.baidu.com"]) {
        
        NSString *myCode =  request.URL.query;
        NSArray *array = [myCode componentsSeparatedByString:@"="];
        
        NSString *code = [array lastObject];
        NSLog(@"%@",code);
        WZUserViewModel *userViewModel = [[WZUserViewModel alloc] init];
        
        [userViewModel getAccessTokenWithCode:code andSuccess:^{
//            发送更换控制器的通知
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            
            [center postNotificationName:@"changeRootVC" object:nil];
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
            NSLog(@"请求成果巩固");
            
        } andFaile:^{
            NSLog(@"请求失败");
        } ];
    }
    
    
    
    
    
    return YES;
}
@end
